package simulator.labyrinth;

import simulator.position.Point;

public abstract class Obstacle {
	private Point start;
	private Point end;
	
	public Obstacle(Point start, Point end) {
		if (start.getX() + start.getY() > end.getX() + end.getY()) {
			this.start = end;
			this.end = start;
		}
		else {
			this.start = start;
			this.end = end;
		}
	}

	public Point getStartPosition() {
		return start;
	}

	public Point getEndPosition() {
		return end;
	}
	
	public double getOrientation() {
		return Math.toDegrees(Math.atan2(getEndPosition().getY() - getStartPosition().getY(), getEndPosition().getX() - getStartPosition().getX()));
	}
	
	public boolean findIntersection(Point position) {
		Point startOfLine = this.getStartPosition();
		Point endOfLine = this.getEndPosition();
		if (startOfLine.getX() == endOfLine.getX() && (Math.abs(startOfLine.getPreciseX()-position.getPreciseX()) <= 1)) {
			if (position.getPreciseY() >= startOfLine.getPreciseY() && position.getPreciseY() <= endOfLine.getPreciseY()) {
				return true;
			}
		}
		else if (startOfLine.getY() == endOfLine.getY() && Math.abs(startOfLine.getPreciseY()-position.getPreciseY()) <= 1) {
			if (position.getPreciseX() >= startOfLine.getPreciseX() && position.getPreciseX() <= endOfLine.getPreciseX()) {
				return true;
			}
		}
		
		return false;
	}
	
	public String toString() {
	    return this.getClass() + " starting at " + getStartPosition() + ", stopping at " + getEndPosition();
	}
}
